package com.fyzk.activity.news;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * @mood
 * @description
 * @author Administrator
 * @date 2018/7/18 14:00
 * </p>
 */
public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context context;
    private LayoutInflater mInflater;
    private List<NewsListBean.DataBean> data;
    private boolean isShowEmpty;

    private static final int TYPE_NULL = 0;
    private static final int TYPE_ITEM = 1;
    private static final int TYPE_FOOTER = 2;

    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;
    //上拉加载更多
    public static final int UP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    //没有更多数据
    public static final int NO_LOAD_MORE = 2;

    private OnItemClickListener listener;
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    public NewsAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<NewsListBean.DataBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public List<NewsListBean.DataBean> getData() {
        if (data == null) {
            data = new ArrayList<>();
        }
        return data;
    }

    public void setHeadData(boolean isShowEmpty) {
        this.isShowEmpty = isShowEmpty;
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowEmpty)
            return TYPE_NULL;
        else
            if (position == data.size())
                return TYPE_FOOTER;
            else
                return TYPE_ITEM;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_NULL) {
            return new NewsNullHolder(mInflater.inflate(R.layout.item_empty, parent, false));
        } else if (viewType == TYPE_ITEM) {
            return new NewsViewHolder(mInflater.inflate(R.layout.item_news, parent, false));
        } else {
            return new NewsFooterViewHolder(mInflater.inflate(R.layout.item_tab_footer_child1, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof NewsNullHolder) {
        } else if (holder instanceof NewsViewHolder) {
            final NewsViewHolder newsViewHolder = (NewsViewHolder) holder;

            newsViewHolder.mTvTitle.setText(data.get(position).getTitle());
            if(!TextUtils.isEmpty(data.get(position).getIcon_url())){
                Glide.with(context).load(data.get(position).getIcon_url())
                        .error(R.drawable.load_failed)
                        .into(newsViewHolder.mIvPic);
            }else{
                newsViewHolder.mIvPic.setVisibility(View.GONE);
            }

            newsViewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(listener != null)
                        listener.onItemClick(newsViewHolder.mCardView, position);
                }
            });

        } else if (holder instanceof NewsFooterViewHolder) {
            NewsFooterViewHolder tabFooterViewHolder = (NewsFooterViewHolder) holder;
            switch (mLoadMoreStatus) {
                case UP_LOAD_MORE:
                    tabFooterViewHolder.mLoadText.setText("上拉加载更多...");
                    break;
                case LOADING_MORE:
                    tabFooterViewHolder.mLoadLy.setVisibility(View.VISIBLE);
                    tabFooterViewHolder.mLoadText.setText("正在加载更多...");
                    break;
                case NO_LOAD_MORE:
                    tabFooterViewHolder.mLoadLy.setVisibility(View.GONE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (isShowEmpty) {
            return 1;
        } else {
            if (data == null) {
                data = new ArrayList<>();
            }
            return data.size() + 1;
        }
    }

    public class NewsNullHolder extends RecyclerView.ViewHolder {
        NewsNullHolder(View itemView) {
            super(itemView);
        }
    }

    public class NewsViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mCardView;
        TextView mTvTitle;
        TextView mTvComment;
        TextView mTvSource;
        ImageView mIvPic;

        NewsViewHolder(View itemView) {
            super(itemView);
            mCardView = itemView.findViewById(R.id.newsItem_cardView);
            mTvTitle = itemView.findViewById(R.id.newsItem_tvTitle);
            mTvComment = itemView.findViewById(R.id.newsItem_tvComment);
            mTvSource = itemView.findViewById(R.id.newsItem_tvSource);
            mIvPic = itemView.findViewById(R.id.newsItem_ivPic);
        }
    }

    public class NewsFooterViewHolder extends RecyclerView.ViewHolder {
        ProgressBar mLoadProgress;
        TextView mLoadText;
        LinearLayout mLoadLy;
        NewsFooterViewHolder(View itemView) {
            super(itemView);
            mLoadProgress = itemView.findViewById(R.id.tabFooterUI_loadProgress);
            mLoadText = itemView.findViewById(R.id.tabFooterUI_loadText);
            mLoadLy = itemView.findViewById(R.id.tabFooterUI_loadLy);
        }
    }

    /**
     * 更新加载更多状态
     * @param status
     */
    public void changeMoreStatus(int status){
        mLoadMoreStatus = status;
        notifyDataSetChanged();
    }

}
